package com.core.framework.web.easyweb.context;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

 

public class HtmlEscape {

	private static final char[] LT = "&lt;".toCharArray();

	private static final char[] GT = "&gt;".toCharArray();

	private static final char[] AMP = "&amp;".toCharArray();

	private static final char[] QUOT = "&quot;".toCharArray();
	public static String  [] escape(String strs[]) {
		 for(int i=0;i<strs.length;i++){
			 strs[i]=escape(strs[i]);
		 }
		 return strs;

	}
	public static void main(String args[]){
		System.out.println(HtmlEscape.escape("<html>ok</html>"));
		System.out.println(backEscape(HtmlEscape.escape("<&lt;lt;&lt;html>ok</html>")));
	}
	public static String backEscape(String str) {
		 
		if(str==null)return null;
		System.out.println(new String(LT));
		 return str.replaceAll(new String(LT), "<").replaceAll(new String(GT), ">");
		}
	
	public static String escape(String str) {
 
		if(str==null)return null;
		StringBuffer buff = new StringBuffer();
		char[] cs = str.toCharArray();
		for (char c : cs) {
			switch (c) {
		
			case '<':
				buff.append(LT);
				break;

			case '>':
				buff.append(GT);
				break;

//			case '&':
//				buff.append(AMP);
//				break;

//			case '"':
//				buff.append(QUOT);
//				break;
			default:
				buff.append(c);
			}
		}
		
		return buff.toString();

	}
	public static String escape(char cs[]) {
		if(cs==null)return null;
		StringBuffer buff = new StringBuffer();
		for (char c : cs) {
			switch (c) {
			case '<':
				buff.append(LT);
				break;
			case '>':
				buff.append(GT);
				break;

//			case '&':
//				buff.append(AMP);
//				break;
//
//			case '"':
//				buff.append(QUOT);
//				break;
			default:
				buff.append(c);
			}
		}
		return buff.toString();

	}
	
	public static Map escape(Map map1) {
		Map map=new HashMap(map1);
	     Set set=new HashSet(map.keySet());
		 for(Object o:set){
			 Object obj=map.get(o);
			 if(obj instanceof String){
				 if(!"jsonpage".equals(o)){
				 map.put(o,escape((String)obj) );
				 }
			 }
			 if(obj instanceof String[]){
				 if(!"jsonpage".equals(o)){
				 map.put(o,escape((String[])obj) );
				 }
			 }
		 }
		 return map;

	}
}
